<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/10
 * Time: 10:43
 */

namespace Article\Controller;
    use Think\Controller;
    use Think\Model;

class ArticleController extends Controller {

    protected $article_mysql;

    public function _initialize(){
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:GET');
        header('Access-Control-Allow-Methods:POST');
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        $this->article_mysql = M("article");
    }
//获取文章的所有评论
//    public function index(){
//        $data=D('ArticleRelation')->relation(true)->find();
//        $result=$data;
//        var_dump($result);
//        die;
//       echo json_encode($result,JSON_UNESCAPED_UNICODE);
//    }
//发布文章
//    public function addarticle(){
//        $token=I('token');
//       //$user=D('UserRelation');
//        $time = date(Ymdhis);
//        $userinfo=array(
//            'title'=>I('title'),
//            'time'=>$time,
//            'author'=>I('author'),
//            'content'=>I('content'),
//            'ps_id'=>md5($time)
//        );
//        $uid=M('user')->where(array('token'=>$token))->find();//返回用户的userid
//        $aid=M('article')->add($userinfo);//返回当前添加进去的id
//
//        $data=array(
//            'userid'=>$uid['userid'],
//            'aid'=>$aid
//        );
//        M('user_article')->add($data);
//    }

    //删除文章
//    public function deletearticle(){
//        $psid=I('psid');
//        $aid=M('article')->where(array('ps_id'=>$psid))->find();
//        M('article')->where(array('ps_id'=>$psid))->delete();
//        M('user_article')->where(array('aid'=>$aid['aid']))->delete();
//    }

    //获取文章
    public function getarticle($id){
        $pageid = $id;//$_GET['psid'];
        $condition["ps_id"]=$pageid;
        $article = $this->article_mysql->where($condition)->select();

        $return_article["code"] = 1;
        if(count($article) == 0){
            $return_article["code"] = -1;
        }else{
            $return_article["value"] = $article[0];
        }
//      echo json_encode($return_article["value"]["content"]);
//        echo "</br>";
//        echo $return_article["value"]["content"];
//        echo "<br>";
//        $obj=json_encode('<a>dsadasdasd</a>',true);
//        echo $obj;
//        echo "<br>";
//        echo json_decode($obj);
//        die;
        return json_encode($return_article,JSON_UNESCAPED_UNICODE);
    }

     //获取文章列表
    public function getarticlelist(){
        $page =(int)$_GET['p'];
        if($page == "")
            $page = 0;
        $articlelist = $this->article_mysql->limit(0+$page*5,5)->field("title,time,author,summary,ps_id,like,readtimes,image")->order('time desc')->select();
        for($i=0;$i<count($articlelist);$i++)
        {
            $articlelist[$i]["time"]=substr( $articlelist[0]["time"], 0, 16);
        }
        $return_article["code"] = 1;
        if(count($articlelist) == 0){
            $return_article["code"] = -1;
        }else{
            $return_article["value"] = $articlelist;
        }

        echo json_encode($return_article,JSON_UNESCAPED_UNICODE);
    }

    //首页获取热帖
    public function hotarticle(){
       $article=M('article')->limit(2)->field('ps_id,title,summary,image')->order('readtimes desc')->select();
       $banner=M('banner')->limit(6)->order('createtime desc')->select();
        $testtimes=M('retimes')->where(array('id'=>1))->select();
        if(!$article||!$banner){
            $result=array(
                'code'=>'0',
                'value'=>'当前没有热帖',
                'banner'=>'',
                'testtimes'=>''
            );
        }
        else{
            $result=array(
                'code'=>'1',
                'value'=>$article,
                'banner'=>$banner,
                'testtimes'=>$testtimes[0]['testtimes']
            );
        }
    echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }

}